package com.lovo.csc.controller;


import com.lovo.csc.dto.PurchaseTenderDto;
import com.lovo.csc.entity.Purchasetender;
import com.lovo.csc.service.IPurchasetenderService;
import com.lovo.util.MQUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-13
 */
@RestController
@RequestMapping("/purchasetender")
@Api(tags = "采购标书控制层--舒得强")
public class PurchasetenderController {

    @Autowired
    IPurchasetenderService purchasetenderService;

    @Autowired
    RabbitTemplate rabbitTemplate;
    @GetMapping("findByIdTender/{tenderId}")
    @ApiOperation(value = "根据标书编号查询标书信息")
    public Purchasetender findByIdTender(@PathVariable("tenderId") int tenderId){
        return purchasetenderService.findByIdTender(tenderId);
    }
    @PostMapping("purchaseTender")
    @ApiOperation(value = "采购标书上传到MQ,以及持久化到本地")
    public boolean purchaseTender(PurchaseTenderDto purchaseTenderDto){
        if (purchaseTenderDto!=null){
            Purchasetender purchasetender = new Purchasetender();
            purchasetender.setPurchasetenderid(purchaseTenderDto.getPurchaseCode());
            purchasetender.setGoodname(purchaseTenderDto.getGoodName());
            purchasetender.setGoodsort(purchaseTenderDto.getGoodSort());
            purchasetender.setGoodinfo(purchaseTenderDto.getGoodInfo());
            purchasetender.setPurchasenum(purchaseTenderDto.getPurchaseNum());
            purchasetender.setPurchasecode( purchasetender.getPurchasetenderid());
            purchasetender.setPurchasestate(purchaseTenderDto.getPurchaseState());
            purchasetenderService.save(purchasetender);
            rabbitTemplate.convertAndSend(MQUtil.tenderDirectExchange,MQUtil.tenderKey,purchaseTenderDto);
        }
        return true;
    }
    @PutMapping("updateTenderState/{tenderId}/{state}")
    @ApiOperation(value = "修改采购标书状态")
    public boolean updateTenderState(@PathVariable("tenderId") int tenderId,@PathVariable("state") String state){
        Purchasetender purchasetender = new Purchasetender();
        purchasetender.setPurchasetenderid(tenderId);
        purchasetender.setPurchasestate(state);
        purchasetenderService.updateById(purchasetender);
        return true;
    }
}
